package cn.blbsz.shop.controller;

import cn.blbsz.shop.domain.p.UserEntity;
import cn.blbsz.shop.domain.p.UserFundEntity;
import cn.blbsz.shop.service.UserFundService;
import cn.blbsz.shop.service.UserService;
import cn.blbsz.shop.utils.R;
import cn.blbsz.shop.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Api(tags = "用户信息接口")
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserFundService userFundService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/home")
    public String index(Model model){
        UserEntity user = null;
        if ((user = SecurityUtil.getCurrentUser()) != null) {
            model.addAttribute("user", user);
            UserFundEntity fund = userFundService.findByUserId(user.getUserId());
            if (fund != null){
                model.addAttribute("fund",fund);
            }
            return "home";
        } else {
            return "error";
        }
    }

    @ApiOperation("获取验证码")
    @GetMapping(value = "/verifycode")
    @ResponseBody
    public R getVerifyCode(String tel) {

        char[] chars = "1234567890".toCharArray();
        Random rand = new Random();
        StringBuffer text = new StringBuffer();

        for (int i = 0; i < 4; i++){
            text.append(chars[rand.nextInt(chars.length)]);
        }

        stringRedisTemplate.opsForValue().set(tel, text.toString(),10, TimeUnit.MINUTES);

        return R.ok("验证码发送成功");
    }

    @ApiOperation("用户注册")
    @PostMapping(value = "/api/register")
    public R handleRegi(String pid, String tel, String verifycode, String password) {
        String text = stringRedisTemplate.opsForValue().get(tel);
        if (text == null || !text.equals(verifycode)) return R.error("验证码错误");

        String msg = userService.handleRegi(pid, tel, password);
        return R.ok(msg);
    }

    //个人中心
    @GetMapping("/user_info")
    @ApiOperation("用户信息")
    @PreAuthorize("hasAnyRole('ROLE_NORMAL')")
    public R getPersonalCenter(Model model) {
        UserEntity user = null;
        if ((user = SecurityUtil.getCurrentUser()) != null) {
            model.addAttribute("user", user);
            UserFundEntity fund = userFundService.findByUserId(user.getUserId());
            if (fund != null){
                model.addAttribute("fund",fund);
            }
            return R.ok().data(model);
        } else {
            return R.error();
        }
    }

}
